/**
 * 
 */
package jvs.peer.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

import jvs.peer.io.ChannelManager;
import jvs.peer.net.PeerServerSocket;

/**
 * @author qiangli
 * 
 */
public class EchoServer {
	public static final String realm = "btspp";

	private static int serverId = 0;
	private int instanceId = 0;
	private ServerSocket listen;

	/**
	 * @throws IOException
	 * 
	 */
	public EchoServer() throws IOException {
		listen = new PeerServerSocket(realm);
		serverId++;
		System.out.println("Echo Server Version 1.0 [" + realm +"]");
		System.out.println("");

	}

	public void service() throws IOException {
		while (true) {
			try {
				String id = serverId + ":" + instanceId;
				System.out.println("Server instance [" + id + "] Ready.");
				Socket s = listen.accept();

				Thread t = new ByteServerThread(s);
				t.start();

				instanceId++;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Properties attrs = System.getProperties();
			
			attrs.put("port", 50001+"");
			
			attrs.put(ChannelManager.CHANNEL_DRIVER_CLASS, "jvs.peer.io.SocketServer");
			
			//attrs.load(new FileInputStream("C:/workspace/jvs2/resource/user2.txt"));
			
			ChannelManager.getInstance(realm).init(attrs);
			ChannelManager.getInstance(realm).startup();
			EchoServer s = new EchoServer();
			s.service();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

class LineServerThread extends Thread {
	private Socket socket = null;

	public LineServerThread(Socket socket) {
		super("LineServerThread");
		this.socket = socket;
	}

	public void run() {
		try {
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));

			String line = null;
			while ((line = in.readLine()) != null) {
				out.println(line);
				
				System.err.println(line);
				
				if (line.equalsIgnoreCase("bye")) {
					break;
				}
			}
			out.close();
			in.close();

			socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class ByteServerThread extends Thread {
	private Socket socket = null;

	public ByteServerThread(Socket socket) {
		super("ByteServerThread");
		this.socket = socket;
	}

	public void run() {
		try {
			OutputStream out = socket.getOutputStream();
			InputStream in = socket.getInputStream();

			byte[] buf = new byte[1024];
			int count = 0;
			while (true) {
				int read = in.read(buf);
				if (read > 0) {
					count += read;
					out.write(buf,0,read);
					out.flush();
					
					System.err.println("count: "+count);
				} else {
					break;
				}
			}
			out.close();
			in.close();
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
